//package com.demo.gateway.excption;
//
//import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.GatewayCallbackManager;
//import com.alibaba.csp.sentinel.slots.block.BlockException;
//import com.alibaba.csp.sentinel.util.function.Supplier;
//import com.alibaba.fastjson.JSON;
//import com.demo.gateway.util.SentinelUtils;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.core.io.buffer.DataBuffer;
//import org.springframework.http.HttpStatus;
//import org.springframework.http.codec.HttpMessageWriter;
//import org.springframework.http.codec.ServerCodecConfigurer;
//import org.springframework.http.server.reactive.ServerHttpResponse;
//import org.springframework.web.reactive.function.server.ServerResponse;
//import org.springframework.web.reactive.result.view.ViewResolver;
//import org.springframework.web.server.ServerWebExchange;
//import org.springframework.web.server.WebExceptionHandler;
//import reactor.core.publisher.Mono;
//
//import java.util.HashMap;
//import java.util.List;
//
///**
// * @author wuyijie
// * @className: CustomGatewayBlockExceptionHandler
// * @description: TODO       方案1：http://www.manongjc.com/detail/20-hwiidrxknzzmgtn.html
// * 方案2 https://segmentfault.com/a/1190000018993355
// * @date 2021/4/1 17:30
// */
//@Slf4j
//public class CustomGatewayBlockExceptionHandler implements WebExceptionHandler {
//
//    private List<ViewResolver> viewResolvers;
//    private List<HttpMessageWriter<?>> messageWriters;
//    private final Supplier<ServerResponse.Context> contextSupplier = () -> {
//        return new ServerResponse.Context() {
//            @Override
//            public List<HttpMessageWriter<?>> messageWriters() {
//                return CustomGatewayBlockExceptionHandler.this.messageWriters;
//            }
//
//            @Override
//            public List<ViewResolver> viewResolvers() {
//                return CustomGatewayBlockExceptionHandler.this.viewResolvers;
//            }
//        };
//    };
//
//    public CustomGatewayBlockExceptionHandler(List<ViewResolver> viewResolvers, ServerCodecConfigurer serverCodecConfigurer) {
//        this.viewResolvers = viewResolvers;
//        this.messageWriters = serverCodecConfigurer.getWriters();
//    }
//
//
//
//    /**
//     * 重写限流响应，改造成JSON格式的响应数据
//     * @author javadaily
//     * @date 2020/1/20 15:03
//     */
//    private Mono<Void> writeResponse(ServerResponse response, ServerWebExchange exchange,Throwable ex) {
//        ServerHttpResponse serverHttpResponse = exchange.getResponse();
////        "application/json;charset=UTF-8"
//        serverHttpResponse.getHeaders().add("Content-Type", "application/json;charset=UTF-8");
//        HashMap<String, Object> result = new HashMap<>(4);
//        result.put("code", HttpStatus.INTERNAL_SERVER_ERROR.value());
//        result.put("msg", SentinelUtils.HandlerException(ex));
//        result.put("data",null);
//
//
//        String resultString = JSON.toJSONString(result);
//        log.error("************  GatewayBlockExceptionHandler msg {}",resultString);
//        DataBuffer buffer = serverHttpResponse.bufferFactory().wrap(resultString.getBytes());
//        return serverHttpResponse.writeWith(Mono.just(buffer));
//    }
//
//    @Override
//    public Mono<Void> handle(ServerWebExchange exchange, Throwable ex) {
//        if (exchange.getResponse().isCommitted()) {
//            return Mono.error(ex);
//        } else {
//            return !BlockException.isBlockException(ex) ? Mono.error(ex) : this.handleBlockedRequest(exchange, ex).flatMap((response) -> {
//                return this.writeResponse(response, exchange, ex);
//            });
//        }
//    }
//
//    private Mono<ServerResponse> handleBlockedRequest(ServerWebExchange exchange, Throwable throwable) {
//        return GatewayCallbackManager.getBlockHandler().handleRequest(exchange, throwable);
//    }
//}
